package fr.cephb.joperon.core.iterator;



import com.sleepycat.db.Cursor;
import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.db.DatabaseException;

/**
 * An iterator iterating over all the values of a unique DatabaseEntry key (multimap)
 * @author lindenb
 *
 */
public class EqualRangeIterator
	extends KeyRangeIterator {
	
	private byte keyBytes[];
	
	/**
	 * Constructor
	 * @param cursor current cursor
	 * @param entry the key we are looking
	 */
	public EqualRangeIterator(Cursor cursor,DatabaseEntry entry)
		{
		super(cursor,entry);
		this.keyBytes=new byte[entry.getSize()];
		System.arraycopy(entry.getData(), 0, keyBytes, 0, keyBytes.length);

		}
	
	@Override
	protected boolean validateCursor() throws DatabaseException
		{
		return isInRange(_nextValue.first());
		}
	
	/** called by validateCursor
	 * test if the current cursor is the same (compare bytes)
	 * as the key defined in the constructor
	 */
	protected boolean isInRange(DatabaseEntry entry)
		{
		byte data[]=entry.getData();
		if(this.keyBytes.length!=entry.getSize()) return false;	
		for (int i=0; i<this.keyBytes.length; i++)
			{
			 if (this.keyBytes[i] != data[i])
                return false;
			}
		return true;
		}
	
	
}
